package com.xdj.service.impl;

import com.xdj.mapper.UserMapper;
import com.xdj.pojo.User;
import com.xdj.service.UserService;
import com.xdj.utils.JwtUtil;
import com.xdj.utils.Md5Util;
import com.xdj.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * ClassName: UserServiceImpl
 * Package: com.xdj.service.impl
 * Description:
 *
 * @Author: 信
 * @Create: 2024/1/11 - 14:18
 */
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserName(String username) {
        User u = userMapper.getByUserName(username);
        return u;
    }

    @Override
    public void register(String username, String password) {
        //加密
        String md5String = Md5Util.getMD5String(password);
        //添加
        userMapper.add(username, md5String);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
/*        Map<String, Object> map = JwtUtil.parseToken(ThreadLocalUtil.get());
        String username = (String) map.get("username");
        User user = userMapper.getByUserName(username);
        user.setUserPic(avatarUrl);*/

        //只有url不行，还需要有用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl, id);
    }

    @Override
    public void updatePwd(String new_pwd) {
        //只有密码不行，还需要有id
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(new_pwd), id);
    }

}
